<?php

namespace app\api\logic;
use app\common\library\Excel;
use app\common\service\UploadService;
use app\data\util\MqQueueName;
use app\systems\logic\MqTaskPushLogic;
use util\Upload;
use think\facade\Db;
class UploadTestLogic extends BasicLogic
{


    /**
     * @desc    上传图片信息
     * @author  luckyXiong
     * @date    2024-07-03
     */
    public function upload_image($file=[]){
        $upload = new Upload();
        $res    = $upload->upload($file);
        $arr    = self::uploadImageOSS($res);
        return  $this->jsonMessage(200,true,'上传成功',$arr);
    }


    /**
     * @desc    上传图片信息
     * @author  luckyXiong
     * @date    2024-07-03
     */
    public function upload_video($file=[]){
        $upload = new Upload();
        $res    = $upload->upload($file);
        $arr    = self::uploadVideoOSS($res);
        return  $this->jsonMessage(200,true,'上传成功',$arr);
    }




    /**
     * @desc    上传图片信息
     * @author  luckyXiong
     * @date    2024-07-03
     */
    public function upload_shopify_image($message){
        $seller_sku = $message['seller_sku']??'';
        $parts      = explode('-', $seller_sku);
        $spu_color  = implode('-', array_slice($parts, 0, 2));
        $message['spu_color'] = $spu_color;
        $oss_url    = self::uploadSkuOSS($message);
        $where      = [];
        $where[]    = ['image_info','<>',''];
        $where[]    = ['seller_sku', 'like', '%' . $spu_color . '%'];
        //批量更新 sku图片信息;
        Db::name('product_seller_sku')->where($where)->update(
          [
              'image_info'=>$oss_url,
              'is_oss'    =>1
          ]
        );
        return  true;
    }



    /**
     * @desc    上传图片信息
     * @author  luckyXiong
     * @date    2024-07-03
     */
    public function upload_urls($params=[]){
        $images    = $params['images_urls']??'';
        $images    = trim($images);
        $images    = array_filter(array_map('trim', explode("\n", $images)));
        $arr       = [];
        foreach($images as $v){
            $arr[] =  self::uploadImageOSS(['url'=>$v]);
        }
        return  $this->jsonMessage(200,true,'上传成功',$arr);
    }


    /**
     * @desc    上传excel信息
     * @author  luckyXiong
     * @date    2024-07-10
     */
    public function uploadExcel($url=''){
        $data             = Excel::getExcelData('.' . '/' . $url);
        $excelData        = isset($data[0]) ? $data[0] : $data;
        if(!$excelData)    return json(DefaultResponse(201, false, '暂无数据!', null));
    }





    /**
     * @desc 本地数据上传OSS
     * @author luckyXiong
     * @Date   2024/03/18 10:55
     */
    public function uploadImageOSS($params=[]){
        $doc_url   = $params['url']??'';
        //地址上传到阿里云OSS;
        $service   = new UploadService();
        $BASE_PATH = dirname(__DIR__, 1);
        $local_dir = $BASE_PATH . '/runtime/images/';
        $unique_key = uniqid();
        if (!is_dir($local_dir)) {
            mkdir($local_dir ,0777 ,true);
        }
        //存入本地地址;
        $local_dir_file = $local_dir.$unique_key.'.png';
        file_put_contents($local_dir_file, file_get_contents($doc_url));
        //上传阿里云地址;
        $ossDir   = 'shopify/images/productImage/' .date('ymd').'/'.$unique_key.'.png';
        $res      = $service->uploads($local_dir_file,$ossDir);
        //上传后清除本地数据
        unlink($local_dir_file);
        return $res['info']['url']??'';
    }


    /**
     * @desc 本地数据上传OSS
     * @author luckyXiong
     * @Date   2024/03/18 10:55
     */
    public function uploadVideoOSS($params=[]){
        $doc_url   = $params['url']??'';
        //地址上传到阿里云OSS;
        $service   = new UploadService();
        $BASE_PATH = dirname(__DIR__, 1);
        $local_dir = $BASE_PATH . '/runtime/images/';
        $unique_key = uniqid();
        if (!is_dir($local_dir)) {
            mkdir($local_dir ,0777 ,true);
        }
        //存入本地地址;
        $local_dir_file = $local_dir.$unique_key.'.mp4';
        file_put_contents($local_dir_file, file_get_contents($doc_url));
        //上传阿里云地址;
        $ossDir   = 'kol/' .date('ymd').'/'.$unique_key.'.mp4';
        $res      = $service->uploadsVedio($local_dir_file,$ossDir);
        //上传后清除本地数据
        unlink($local_dir_file);
        return $res['info']['url']??'';
    }



    /**
     * @desc    本地数据上传OSS
     * @author luckyXiong
     * @Date   2025-03-04 10:55
     */
    public function uploadSkuOSS($params){
        $doc_url     = $params['url']??'';
        $spu_color   = $params['spu_color']??'';
        //地址上传到阿里云OSS;
        $service   = new UploadService();
        $BASE_PATH = dirname(__DIR__, 1);
        $local_dir = $BASE_PATH . '/runtime/images/';
        $unique_key = uniqid();
        if (!is_dir($local_dir)){
            mkdir($local_dir ,0777 ,true);
        }
        //存入本地地址;
        $local_dir_file = $local_dir.$unique_key.'.png';
        file_put_contents($local_dir_file, file_get_contents($doc_url));
        //上传阿里云地址;
        $spu_color = str_replace(' ', '', $spu_color);
        $ossDir    = 'shopify/images/skuImages/' .$spu_color.'/'.$unique_key.'.png';
        $res       =  $service->uploads($local_dir_file,$ossDir);
        //上传后清除本地数据
        unlink($local_dir_file);
        return $res['info']['url']??'';
    }


    /**
     * @desc    sku图片更新生产方法
     * @author  luckyXiong
     * @date    2025-03-07
     */
    public function uploadUrlImagesproduce($params){
           $where   = [];
           $where[] = ['image_info','<>',''];
           $where[] = ['is_oss','=','0'];
           $list    = Db::name('product_seller_sku')
                      ->field('id,spu,seller_sku,image_info as url')
                      ->where($where)
                      ->group('image_info')
                      ->select()
                      ->toArray();

           if($list){
                 foreach($list as $v){
                     // 队列处理
                     MqTaskPushLogic::mq_task_push(
                         '',
                         MqQueueName::QUEUE_PRODUCT_IMAGE_SKU_QUEUE,
                         $v,
                         false
                     );
                 }
           }
           return  $this->jsonMessage(200,true,'操作成功','');
    }
    
}